//
//  headerCollectionView.m
//  FairyDream
//
//  Created by dllo on 15/8/25.
//  Copyright (c) 2015年 dreams. All rights reserved.
//

#import "headerCollectionView.h"
#import "RotationCell.h"
#import "UIImageView+WebCache.h"
#import "FourPicture.h"
#define Width self.frame.size.width
#define collectionSection 100
#define ScreenWidth  [UIScreen mainScreen].bounds.size.width
@interface headerCollectionView ()<UICollectionViewDataSource, UICollectionViewDelegate>

@property (nonatomic, retain) UIPageControl *pageControl;
@property (nonatomic, retain) NSTimer *timer;
@end


@implementation headerCollectionView

- (void)dealloc
{
    [super dealloc];
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.pictureArray = [NSMutableArray array];
        
        [self createCollectionView];
        FourPicture *four = [[FourPicture alloc] initWithFrame:CGRectMake(0, 200, self.frame.size.width, 120)];
        
        [self addSubview:four];
        
    }
    return self;
}


- (void)createCollectionView
{
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.itemSize = CGSizeMake(Width, 200);
    // 左右间距
    layout.minimumLineSpacing = 0;
    // 横向滑动
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, Width, 200) collectionViewLayout:layout];
    _collectionView.backgroundColor = [UIColor yellowColor];
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    [_collectionView registerClass:[RotationCell class] forCellWithReuseIdentifier:@"picture"];
    _collectionView.pagingEnabled = YES;
    [self addSubview:_collectionView];
    [layout release];
//    [self addTimer];
    [_collectionView release];
    
}

- (void)createPage
{
    self.pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width / 2, 20)];
    self.pageControl.center = CGPointMake(self.frame.size.width / 2, self.frame.size.height - 20 - 120);
    
    [self addSubview:self.pageControl];
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    self.pageControl.numberOfPages = _pictureArray.count;
    return _pictureArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    RotationCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"picture" forIndexPath:indexPath];
    if (_pictureArray.count != 0) {
        NSDictionary *dic = [_pictureArray objectAtIndex:indexPath.item];
        [cell.pictureView sd_setImageWithURL:[NSURL URLWithString:[dic objectForKey:@"photo"]] placeholderImage:nil];
    }
    return cell;
}
- (void)addTimer
{
    NSTimer *timer = [NSTimer timerWithTimeInterval:4 target:self selector:@selector(imageNext) userInfo:nil repeats:YES];
    [[NSRunLoop currentRunLoop] addTimer:timer forMode:NSDefaultRunLoopMode];
    self.timer = timer;
}


- (void)removeTimer
{
    // 释放
    [self.timer invalidate];
    self.timer = nil;
}

- (NSIndexPath *)returnIndexPath
{
   
    NSIndexPath *currentIndexPath = [[self.collectionView indexPathsForVisibleItems] lastObject];
    currentIndexPath = [NSIndexPath indexPathForRow:currentIndexPath.row inSection:collectionSection / 2];
    [self.collectionView scrollToItemAtIndexPath:currentIndexPath atScrollPosition:UICollectionViewScrollPositionLeft animated:NO];
    return currentIndexPath;
}

- (void)imageNext
{
    NSIndexPath *indexPath = [self returnIndexPath];
    NSInteger item = indexPath.row + 1;
    NSInteger section = indexPath.section;
    if (item == _pictureArray.count) {
        item = 0;
        section ++;
    }
    
    self.pageControl.currentPage = item;
    NSIndexPath *nextIndexPath = [NSIndexPath indexPathForRow:item inSection:section];
    [self.collectionView scrollToItemAtIndexPath:nextIndexPath atScrollPosition:UICollectionViewScrollPositionLeft animated:YES];
}
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    [self removeTimer];
}

//- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
//{
//    
//}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    NSInteger page = (NSInteger)(scrollView.contentOffset.x / (ScreenWidth - 20)) % self.pictureArray.count;
    self.pageControl.currentPage = page;
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return collectionSection;
}


@end
